<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<div id="submitDialog" class="container-fluid">
    <div class="row">
        <form id="createForm" method="post" class="form-horizontal" action="" enctype="multipart/form-data">
            <input type="hidden" class="form-control" name="id" value="${log.id}" />
            <div class="form-group">
                <label class="col-sm-4 control-label" for="status">
                    <span class="help-inline"><font color="red">*</font> </span>
                    状态：</label>
                <div class="col-sm-5">
                    <select id="status" name="status" class="form-control" placeholder="状态">
                        <option value="3" selected="selected">已完成</option>
                        <option value="4" >失败</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="certificate">
                    上传凭证：</label>
                <div class="col-sm-5">
                    <div id="picker">上传凭证</div>
                    <div id="pickerInfo"></div>
                    <input type="hidden" class="form-control" id="certificate" name="certificate" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="bz">
                    <span class="help-inline"><font color="red">*</font> </span>
                    描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="bz" name="bz" placeholder="描述"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="submitDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    var rules = {
        status: "required",
        certificate: {
            required: true
        },
        bz: {
            required: true
        }
    };
    var messages = {
        status: "该项是必填项。",
        certificate: {
            required: "该项是必填项。"
        },
        bz: {
            required: "该项是必填项。"
        }
    };
    function validateForm(){
        return $( "#createForm" ).validate( {
            rules: rules,
            messages: messages,
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } )
    }

    $(function(){
        $('#status').change(function() {
            var status = $(this).val();
            if (status === 3){
                rules.certificate = {
                    required: true
                };
                messages.certificate = {
                    required: "该项是必填项。"
                };
            } else {
                delete rules.certificate;
                delete messages.certificate;
            }
        });
        initUploader();
    });

    function initUploader() {
        //百度上传按钮
        var uploader = WebUploader.create({
            // swf文件路径
            swf: '${basePath}/resources/zheng-admin/plugins/webuploader-0.1.5/Uploader.swf',
            // 文件接收服务端
            server: '${basePath}/manage/billlog/upload/${log.id}',
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: {
                "id": '#picker',
                "multiple": false
            },
            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false,
            // 选完文件后，是否自动上传。
            auto: false,
            // 只允许选择图片文件
            accept: {
                title: '图片文件',
                extensions: 'gif,jpg,jpeg,png',
                mimeTypes: 'image/*'
            }
        });
        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#pickerInfo' ),
                $percent = $li.find('.progress span');

            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<p class="progress"><span></span></p>')
                    .appendTo( $li )
                    .find('span');
            }

            $percent.css( 'width', percentage * 100 + '%' );
        });
        uploader.on( 'fileQueued', function(file) {
            uploader.upload();
        });
        uploader.on( 'uploadSuccess', function(file, response) {
            $( '#pickerInfo' ).addClass('upload-state-done');
            $( '#pickerInfo' ).text(file.name);
            $('#certificate').val(response.data);
        });

        // 文件上传失败，显示上传出错。
        uploader.on( 'uploadError', function( file ) {
            var $li = $( '#pickerInfo'),
                $error = $li.find('div.error');

            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }

            $error.text('上传失败');
        });
        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on( 'uploadComplete', function( file ) {
            $( '#pickerInfo' ).find('.progress').remove();
        });
    }


    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/billlog/submit',
                data: $('#createForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        infoDialog(result.data);
                    } else {
                        submitDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>